package org.cttic.visualization.visualization.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.itineraryQueryData;
import org.cttic.visualization.visualization.common.trackQueryData;
import org.cttic.visualization.visualization.service.TrackQuery;
import org.cttic.visualization.visualization.util.DataUtil;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-30 13:39
 * 2.3.2车辆轨迹点查询接口
 */
@Service
public class TrackQueryImpl implements TrackQuery {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public String trackQuery(String jsonData) {
        JSONObject Data = ParseJsonDataUtil.getJsonData(jsonData);
        String provinceId = Data.getString("provinceId");
        //车牌_颜色
        String vehiclesId = Data.getString("vehiclesId");
        String[] split = vehiclesId.split("_");
        String vehPlate=split[0];
        String vehPlateColor =split[1];

        String queryType = Data.getString("queryType");

        String queryValue1 = Data.getString("queryValue1");
        if(queryType.equals("2")){
            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongolocation)
                    .find(Filters.and(Filters.eq("vehicleNo", vehPlate)
                            , Filters.eq("vehilceColor", Integer.parseInt(vehPlateColor)),
                            Filters.eq("endLat", queryValue1)));
            List<trackQueryData.vehLocation> vehLocations = DataUtil.trackQueryDataUtil(documents);
            trackQueryData trackQueryData = new trackQueryData();
            trackQueryData.setVehLocationList(vehLocations);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(trackQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        }else if (queryType.equals("1")){
            String queryValue2 = Data.getString("queryValue2");     //结束时间
            String startTime = DateUtil.TtoTime(queryValue1);
            String endTime = DateUtil.TtoTime(queryValue2);
            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongolocation)
                    .find(Filters.and(Filters.eq("vehicleId", vehPlate),
                            Filters.eq("vehicleIdColor", vehPlateColor),
                            Filters.lte("entryTime",endTime),
                            Filters.gte("entryTime",startTime)));
            List<trackQueryData.vehLocation> list = DataUtil.trackQueryDataUtil(documents);

            trackQueryData trackQueryData = new trackQueryData();
            trackQueryData.setVehLocationList(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(trackQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else if (queryType.equals("0")){
            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongolocation)
                    .find(Filters.and(Filters.eq("vehicleId", vehPlate),
                            Filters.eq("vehicleIdColor", vehPlateColor),
                            Filters.gte("entryTime",queryValue1)));
            List<trackQueryData.vehLocation> list = DataUtil.trackQueryDataUtil(documents);

            trackQueryData trackQueryData = new trackQueryData();
            trackQueryData.setVehLocationList(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(trackQueryData);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}
